let axios = require('axios').default;
module.exports = {
    identify: function(baseUrl, proxy) {
        return get(baseUrl, { verb: "Identify" }, proxy)
            .then(response => {
                return Promise.resolve(response.data)
            })
            .catch(err => {
                return Promise.reject(err);
            });
    },

    listSets: function(baseUrl, resumptionToken, proxy) {
        return get(baseUrl, {
                verb: 'ListSets',
                resumptionToken: resumptionToken
            }, proxy)
            .then(response => { return Promise.resolve(response.data) })
            .catch(err => Promise.reject(err));
    },

    listRecords: function(baseUrl, params, proxy) {
        params.verb = `ListRecords`;
        return get(baseUrl, params, proxy)
            .then(response => { return Promise.resolve(response.data) })
            .catch(err => { return Promise.reject(err) })
    },
}

function get(baseUrl, params, proxy) {
    const src = axios.CancelToken.source();
    const timeout = setTimeout(() => {
        src.cancel();
        // Timeout Logic
    }, 120000);
    return axios({
        method: 'get',
        url: baseUrl,
        params: params,
        headers: {
            'User-Agent': 'Mix',
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        proxy: proxy,
        timeout: 2 * 60 * 1000,
        cancelToken: src.token // timeout bug walkaround;
    }).then(data => {
        clearTimeout(timeout);
        return Promise.resolve(data);
    }).catch(err => {
        return Promise.reject(err);
    })
}